import sys
from zlib import decompress
from nntplib import NNTP_SSL, NNTP_SSL_PORT, _GLOBAL_DEFAULT_TIMEOUT, _parse_overview

try:
    import yenc
except ImportError:
    sys.stderr.write('yEnc python module not found. Go to the src/yenc/ directory and type: "python setup.py install\n"')
    sys.exit(1)

class Compressed_NNTP_SSL(NNTP_SSL):
    def __init__(self, host, port=NNTP_SSL_PORT,
                 user=None, password=None, ssl_context=None,
                 readermode=None, usenetrc=False,
                 timeout=_GLOBAL_DEFAULT_TIMEOUT):
        NNTP_SSL.__init__(self, host, port, user, password, ssl_context,
            readermode, usenetrc, timeout)

    def xzver(self, start, end, *, file=None):
        resp, lines = self._longcmd('XZVER {0}-{1}'.format(start, end), file)
        fmt = self._getoverviewfmt()
        decoded_buffer = yenc.decode(lines)
        lines = decompress(decoded_buffer, -15)
        lines = [line.decode(self.encoding, self.errors) for line in lines.split(b'\r\n') if len(line) > 2]
        return resp, _parse_overview(lines, fmt)
